package physicalobjects;

import forces.Force;
import simulationhandlers.Simulation;


/**
 * A muscle is a spring that applies an oscillating force
 * 
 * @author Kathleen Lan, Seon Kang
 */

public class Muscle extends Spring {
    private double myAmplitude;
    private Mass myStart;
    private Mass myEnd;
    private double myLength;
    private double myK;
    private double myPeriodicMuscleFunction;
    private int myTimeCounter = 1;
    private  final int myFrequency = 10;

    /**
     * @param start 
     * @param end 
     * @param length 
     * @param kVal 
     * @param amplitude 
     */
    public Muscle (Mass start, Mass end, double length, double kVal,
            double amplitude) {
        super(start, end, length, kVal);
        myStart = start;
        myEnd = end;
        myLength = length;
        myK = kVal;
        myAmplitude = amplitude;

    }

    @Override
    public void update (Simulation canvas, double dt) {
        updateMyPeriodicMuscleFunction(myTimeCounter);
        myTimeCounter++;
        double[] dxdyPair = getChange(myStart, myEnd);

        // apply hooke's law to each attached mass
        Force f = new Force(Force.angleBetween(dxdyPair[0], dxdyPair[1]), myK *
                (myLength + myPeriodicMuscleFunction - Force.distanceBetween(
                        dxdyPair[0], dxdyPair[1])));
        applyForce(f);
    }

    private void updateMyPeriodicMuscleFunction (int timeT) {
        myPeriodicMuscleFunction = Math.sin(myFrequency * timeT) * myAmplitude;
    }
}
